import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:provider/provider.dart';

import '../../../data/models/bt_goods.dart';
import '../../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../../widgets/pagination/bt_no_more_items_indicator.dart';
import '../../profile/models/bt_coin_shop_model.dart';
import '../../profile/widgets/bt_goods_item_widget.dart';

showInPurchaseModal(BuildContext context) {
  showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    builder: (_) => ChangeNotifierProvider(
      create: (context) => BtCoinShopModel(),
      child: Consumer<BtCoinShopModel>(builder: (context, model, _) {
        return PointerInterceptor(
          child: Container(
            height: 550,
            padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
            decoration: BoxDecoration(
              color: const Color(0xFFF6F7F8),
              borderRadius: BorderRadius.circular(18),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      padding: const EdgeInsets.only(left: 10, right: 8, top: 5, bottom: 3),
                      decoration: BoxDecoration(
                          color: const Color(0xFF3F5FCF), borderRadius: BorderRadius.circular(2)),
                      child: Row(
                        children: [
                          Image.asset(
                            'assets/images/bt_icon_coin.png',
                            width: 24,
                            height: 21,
                          ),
                          const SizedBox(width: 6),
                          Text(
                            '${model.availableCoins}',
                            style: const TextStyle(color: Color(0xFFFFFFFF), fontSize: 16),
                          ),
                        ],
                      ),
                    ),
                    InkWell(
                      child: Image.asset(
                        'assets/images/bt_icon_close.png',
                        width: 28,
                        height: 28,
                      ),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                ),
                Expanded(
                  child: Container(
                      margin: const EdgeInsets.only(top: 8),
                      child: RefreshIndicator(
                        onRefresh: () => Future.sync(
                          () => model.pagingController.refresh(),
                        ),
                        child: MediaQuery.removePadding(
                          context: context,
                          removeTop: true,
                          child: PagedListView<int, BtGoods>(
                            pagingController: model.pagingController,
                            builderDelegate: PagedChildBuilderDelegate<BtGoods>(
                              itemBuilder: (context, item, index) {
                                return Column(
                                  children: [
                                    const Divider(
                                      height: 15,
                                      thickness: 1,
                                      color: Colors.transparent,
                                    ),
                                    BtGoodsItemWidget(index: index, item: item, model: model)
                                  ],
                                );
                              },
                              firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                                error: model.pagingController.error,
                                onTryAgain: () => model.pagingController.refresh(),
                              ),
                              newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                                error: model.pagingController.error,
                                onTap: () => model.pagingController.retryLastFailedRequest(),
                              ),
                              firstPageProgressIndicatorBuilder: (_) => const BtFirstPageProgressIndicator(),
                              newPageProgressIndicatorBuilder: (_) => const Padding(
                                padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                child: BtNewPageProgressIndicator(),
                              ),
                              noItemsFoundIndicatorBuilder: (_) => const Padding(
                                padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                child: BtNoItemsFoundIndicator(),
                              ),
                              noMoreItemsIndicatorBuilder: (_) => const Padding(
                                padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                                child: BtNoMoreItemsIndicator(),
                              ),
                            ),
                          ),
                        ),
                      )),
                )
              ],
            ),
          ),
        );
      }),
    ),
  );
}
